const path = require('path')
const fs = require('fs-extra');
const {
  db
} = require('../utils');
const {
  saveDoc
} = require('../service/save');
const log = require('./../utils/log')
const mime = require('mime')

async function readImagesToBase64(imgs) {
  var map = {};
  var data = Object.keys(imgs);

  for (let index = 0; index < data.length; index++) {
    const url = data[index];
    var baseString = await fs.readFileSync(url, 'base64');
    fs.writeFileSync('./dd.text', `data:${mime.getType(url)};base64,${baseString}`)
    map[url] = `data:${mime.getType(url)};base64,${baseString}`
  }
  return map;
}
module.exports = async function (args) {


  var aPath = path.join(process.cwd(), args.path)
  if (fs.existsSync(aPath)) {
    if (fs.lstatSync(aPath).isDirectory()) {
      log.danger("请指定上传文件")
      return;
    }
    var baseInfo = path.parse(args);
    var dir = baseInfo.dir.split(path.sep).pop()
    var body = fs.readFileSync(aPath, 'utf8')
    var imgData = {};
    var getRepos = await db.getVal(dir);
    var docInfo = getRepos[baseInfo.base];

    var data = await saveDoc(docInfo.parentId, docInfo.id, {
      title: docInfo.title,
      slug: docInfo.slig,
      public: 0,
      body,
    });
    log.success('同步完成');
    // console.log(data)

  } else {
    log.danger("输入路径有误～～")
  }
}